package com.yylending.las.base;

import android.support.v7.widget.GridLayoutManager;

import com.yyfax.common.recyclerview.adapter.MultiItemTypeAdapter;
import com.yyfax.common.recyclerview.listener.OnLoadMoreListener;
import com.yyfax.common.recyclerview.listener.OnRefreshListener;
import com.yyfax.common.recyclerview.view.IRecyclerView;
import com.yyfax.common.recyclerview.wrapper.StatesAdapter;
import com.yyfax.common.recyclerview.wrapper.WrapperAdapter;


/**
 * Created by HenryZhang on 2017/4/11.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public abstract class RecyclerListActivity extends BaseActivity implements
		StatesAdapter.RetryClickListener, OnRefreshListener, OnLoadMoreListener, MultiItemTypeAdapter.OnItemClickListener {

	public IRecyclerView mRecyclerView;
	public WrapperAdapter mWrapAdapter;

	public int mPageNum = 1;
	public int mPageSize = 20;

	public void setup(RecyclerListActivity aty, int recyclerViewId, int spanCount, WrapperAdapter adapter) {
		mRecyclerView = (IRecyclerView) findViewById(recyclerViewId);
		mRecyclerView.setLayoutManager(new GridLayoutManager(aty, spanCount));
		adapter.setOnItemClickListener(aty);
		mWrapAdapter.setRetryClickListener(this);
		mRecyclerView.setAdapter(adapter);
		mRecyclerView.setOnRefreshListener(aty);
		mRecyclerView.setOnLoadMoreListener(aty);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onFunctionalError(Throwable e, boolean autoTips, int functionalCode) {
		super.onFunctionalError(e, autoTips, functionalCode);
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mRecyclerView.hideFooterLoadMoreView();
				mRecyclerView.stopRefreshing();
				mWrapAdapter.setState(StatesAdapter.STATE_ERROR);
			}
		});
	}
}
